using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Specialized;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
using Server = Microsoft.SqlServer.Management.Smo.Server;
	internal class ScriptDatabaseCommand : ScriptObjectCommand
	{
		private const string _enableFullTextScript = "IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))\r\nbegin\r\nEXEC [dbo].[sp_fulltext_database] @action = 'enable'\r\nend\r\n";

		public ScriptDatabaseCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptAction) : base(server, scriptTarget, scriptAction)
		{
		}

		protected override void ScriptCreate(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTarget)
		{
			Database database = (Database)smoObject;
			StringCollection stringCollection = new StringCollection();
			try
			{
				if (database.FullTextCatalogs.Count > 0)
				{
					stringCollection.Add("IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))\r\nbegin\r\nEXEC [dbo].[sp_fulltext_database] @action = 'enable'\r\nend\r\n");
				}
			}
			catch (UnsupportedFeatureException)
			{
			}
			SqlServerVersion targetServerVersion = options.TargetServerVersion;
			options.TargetServerVersion = SqlServerVersion.Version80;
			ScriptDatabaseCommand.AddScriptForExtendedProperties(options, database, stringCollection, "ScriptCreateInternal");
			options.TargetServerVersion = targetServerVersion;
			if (stringCollection.Count > 0)
			{
				scriptTarget(new ObjectInfo(database.Name, database.Urn), stringCollection, ScriptActions.Create);
			}
		}

		protected override void ScriptDrop(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTarget)
		{
			Database database = (Database)smoObject;
			StringCollection stringCollection = new StringCollection();
			SqlServerVersion targetServerVersion = options.TargetServerVersion;
			options.TargetServerVersion = SqlServerVersion.Version80;
			ScriptDatabaseCommand.AddScriptForExtendedProperties(options, database, stringCollection, "ScriptDropInternal");
			options.TargetServerVersion = targetServerVersion;
			if (stringCollection.Count > 0)
			{
				scriptTarget(new ObjectInfo(database.Name, database.Urn), stringCollection, ScriptActions.Drop);
			}
		}

		protected override void ScriptPermission(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTarget)
		{
		}

		private static void AddScriptForExtendedProperties(ScriptingOptions options, Database database, StringCollection createScript, string methodName)
		{
			foreach (ExtendedProperty extendedProperty in database.ExtendedProperties)
			{
				StringCollection scriptWithoutUseStatement = ScriptDatabaseCommand.GetScriptWithoutUseStatement(options, extendedProperty, methodName);
				foreach (string current in scriptWithoutUseStatement)
				{
					createScript.Add(current);
				}
			}
		}

		private static StringCollection GetScriptWithoutUseStatement(ScriptingOptions options, ExtendedProperty extendedProperty, string methodName)
		{
			StringCollection stringCollection = new StringCollection();
			ReflectionUtilities.InvokeMethod(extendedProperty, methodName, new object[]
			{
				stringCollection,
				options
			});
			if (stringCollection.Count > 0 && stringCollection[0].StartsWith("use", StringComparison.OrdinalIgnoreCase))
			{
				stringCollection.Remove(stringCollection[0]);
			}
			return stringCollection;
		}
	}
}
